package com.alit.face.koala.websocket;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import com.alit.face.koala.util.LoggerMessage;
import com.alit.face.koala.util.LoggerQuene;

/**
 * @author litong
 * @date 2019年4月17日_下午3:43:50
 * @version 1.0
 */
// @Component
public class WebLogWebSocket {
  @Autowired
  private SimpMessagingTemplate messagingTemplate;

  /**
   * 推送日志
   */
  @PostConstruct
  public void pushLog() {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    Runnable task = new Runnable() {
      @Override
      public void run() {
        while (true) {
          try {
            LoggerMessage message = LoggerQuene.getInstance().poll();
            if (message != null) {
              messagingTemplate.convertAndSend("/topic/pushLog", message);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    };
    executorService.submit(task);
    executorService.submit(task);
  }
}
